package com.stationbuilding.activity.settings;

import android.app.Application;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.stationbuilding.R;
import com.stationbuilding.activity.WelcomeActivity;
import com.stationbuilding.activity.password.PasswordActivity;
import com.stationbuilding.activity.suggestion.SuggestionActivity;
import com.stationbuilding.utils.AlarmManagerUtils;
import com.stationbuilding.utils.CacheUtil;
import com.stationbuilding.utils.DateUtils;
import com.stationbuilding.utils.PreferenceHelper;

import java.util.Calendar;
import java.util.TimeZone;

public class SettingsActivity extends AppCompatActivity {
    private TextView cache_size;
    private AlarmManagerUtils alarmManagerUtils;
    private TextView set_time;
    private PreferenceHelper preferenceHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        preferenceHelper = PreferenceHelper.getInstance(SettingsActivity.this);

        // 初始化toolbar
        initToolbar();
        // 初始化事件
        initEvent();
    }

    private void initToolbar() {
        // 点击事件监听监听
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.back:
                        finish();
                        break;
                }
            }
        };
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView back = (ImageView) toolbar.findViewById(R.id.back);
        TextView title = (TextView) toolbar.findViewById(R.id.title);
        title.setText(getResources().getText(R.string.settings));
        back.setOnClickListener(onClickListener);
    }

    private void initEvent() {
        // 点击事件监听监听
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.change_password:
                        startActivity(new Intent(SettingsActivity.this, PasswordActivity.class));
                        break;
                    case R.id.update:
                        Toast.makeText(SettingsActivity.this, "更新", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.clear_cache:
                        clearCache();
                        Toast.makeText(SettingsActivity.this, "清除缓存", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.suggestion:
                        startActivity(new Intent(SettingsActivity.this, SuggestionActivity.class));
                        break;
                    case R.id.set_remind:
                        selectDate();
                        break;
                }
            }
        };
        LinearLayout changePassword = (LinearLayout) findViewById(R.id.change_password);
        LinearLayout update = (LinearLayout) findViewById(R.id.update);
        LinearLayout clearCache = (LinearLayout) findViewById(R.id.clear_cache);
        LinearLayout suggestion = (LinearLayout) findViewById(R.id.suggestion);
        LinearLayout set_remind=(LinearLayout) findViewById(R.id.set_remind);
        set_time = (TextView) findViewById(R.id.set_time);
        changePassword.setOnClickListener(onClickListener);
        update.setOnClickListener(onClickListener);
        clearCache.setOnClickListener(onClickListener);
        suggestion.setOnClickListener(onClickListener);
        set_remind.setOnClickListener(onClickListener);
        cache_size= clearCache.findViewById(R.id.cache_size);
        cache_size.setText(CacheUtil.getTotalCacheSize(SettingsActivity.this));
        String remindTime = preferenceHelper.getRemindTime();
        if (remindTime!=null && !remindTime.equals("")){
            set_time.setText(remindTime);
        }
    }
    private void clearCache(){
        CacheUtil.clearAllCache(SettingsActivity.this);
        cache_size.setText("无");
    }

    private void selectDate() {
        final Calendar mCalendar = Calendar.getInstance();
        new TimePickerDialog(SettingsActivity.this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        //获取当前毫秒值
                        long systemTime = System.currentTimeMillis();
                        //是设置日历的时间，主要是让日历的年月日和当前同步
                        mCalendar.setTimeInMillis(System.currentTimeMillis());
                        // 这里时区需要设置一下，不然可能个别手机会有8个小时的时间差
                        mCalendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                        //设置在几点提醒  设置的为选择的小时
                        mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        //设置在几分提醒  设置的为选择的分钟
                        mCalendar.set(Calendar.MINUTE, minute);
                        mCalendar.set(Calendar.SECOND, 0);
                        mCalendar.set(Calendar.MILLISECOND, 0);
                        long selectTime = mCalendar.getTimeInMillis();
                        // 如果当前时间大于设置的时间，那么就从第二天的设定时间开始
                        if(systemTime > selectTime) {
                            mCalendar.add(Calendar.DAY_OF_MONTH, 1);
                        }
                        set_time.setText(hourOfDay+":"+minute);
                        preferenceHelper.saveRemindTime(hourOfDay+":"+minute);
                        alarmManagerUtils= AlarmManagerUtils.getInstance(SettingsActivity.this);
                        alarmManagerUtils.createGetUpAlarmManager();
                        alarmManagerUtils.setStartTime(mCalendar.getTimeInMillis());//设置提醒时间
                        alarmManagerUtils.getUpAlarmManagerStartWork();//开始
                        //        alarmManagerUtils.getUpAlarManagerStopWork();//结束
                    }
                }
                // 设置初始时间
                , mCalendar.get(Calendar.HOUR_OF_DAY)
                , mCalendar.get(Calendar.MINUTE)
                // true表示采用24小时制
                ,true).show();
//                    endDate.setText(DateUtils.formatDate(year, month, dayOfMonth));




    }
}
